package com.tm.szb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.szb.dto.DishDTO;
import com.tm.szb.dto.DishFlavorDTO;
import com.tm.szb.entity.Dish;
import com.tm.szb.entity.DishFlavor;
import com.tm.szb.mapper.DishMapper;
import com.tm.szb.service.DishFlavorService;
import com.tm.szb.service.DishService;
import com.tm.szb.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    private DishFlavorService dishFlavorService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDish(DishDTO dto) {

        //保存菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        baseMapper.insert(dish);

        Long dishId = dish.getId();

        List<DishFlavorDTO> flavors = dto.getFlavors();
        List<DishFlavor> dishFlavorList = flavors.stream().map(dishFlavorDTO -> {
            DishFlavor dishFlavor = new DishFlavor();
            dishFlavor.setDishId(dishId);
            dishFlavor.setName(dishFlavorDTO.getName());
            dishFlavor.setValue(dishFlavorDTO.getValue());
            dishFlavor.setIsDeleted(dishFlavorDTO.getShowOption() ? 1 : 0);
            return dishFlavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(dishFlavorList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delDish(List<Long> ids) {
        dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>().in(DishFlavor::getDishId,ids));
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public DishVO findById(Long id) {
        Dish dish = baseMapper.selectById(id);
        List<DishFlavor> list = dishFlavorService.list(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, id));
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDish(DishDTO dishDTO) {
        Long id = dishDTO.getId();
        dishFlavorService.remove(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId,id));
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        int count = baseMapper.updateById(dish);
        List<DishFlavorDTO> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            List<DishFlavor> dishFlavors = flavors.stream().map(dto -> {
                DishFlavor dishFlavor = new DishFlavor();
                BeanUtils.copyProperties(dto, dishFlavor);
                dishFlavor.setIsDeleted(dto.getShowOption() ? 1 : 0);
                dishFlavor.setDishId(id);
                return dishFlavor;
            }).collect(Collectors.toList());
            dishFlavorService.saveBatch(dishFlavors);
        }

        return count;
    }

    @Override
    public List<DishVO> dishList(Long cateGoryId) {
        List<Dish> dishes = baseMapper.selectList(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, cateGoryId));
        return dishes.stream().map(dish -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            return dishVO;
        }).collect(Collectors.toList());
    }


}
